/*
 * "UrlLib.java" is part of Tarinca-COA
 * (http://code.google.com/p/taringa-api/)
 * Copyright (C) - 2010 Juan B Cabral <jbc dot develop at gmail dot com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package taringa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Clase para realicar peticiones HTTP
 *
 * @version 0.2
 * @license LGPL3
 * @author JBC
 * @since 0.2
 * @date 2010-02-13
 */
abstract class UrlLib {

    /**
     * Realiza una peticion http y retorna el resultado como String
     * @param url la direccion a la cual hacer la peticion
     * @return La respuesta de la peticion http
     * @throws MalformedURLException
     * @throws IOException
     */
    public synchronized static String urlOpen(String url) throws MalformedURLException, IOException {
        String response = "";
        BufferedReader reader = null;
        try {
            InputStream is = new URL(url).openStream();
            InputStreamReader isr = new InputStreamReader(is);
            reader = new BufferedReader(isr);
            String inputLine;
            while ((inputLine = reader.readLine()) != null) {
                response += inputLine;
            }
        } finally {
            reader.close();
        }
        return response;
    }
}
